package com.qf.service.Impl;

import com.qf.dao.GoodsDao;
import com.qf.dao.impl.GoodsDaoImpl;
import com.qf.pojo.PageBean;
import com.qf.pojo.TbGoods;
import com.qf.pojo.TbGoodsKz;
import com.qf.service.GoodsService;
import com.qf.utils.DBUtils;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    GoodsDao goodsDao = new GoodsDaoImpl();
    @Override
    public void addGoods(TbGoodsKz tbGoodsKz) throws Exception {
        long kzid = goodsDao.addGoodsKz(tbGoodsKz);
        long id = goodsDao.addGoods(kzid);
        DBUtils.close();
        System.out.println(id);
    }

    @Override
    public PageBean<TbGoods> getGoodsPageByTypeId(int typeId, int currentPage) throws Exception {
        PageBean<TbGoods> pageBean = new PageBean<>();
        //查询所有的数量
        int goodsCountByTypeId = goodsDao.getGoodsCountByTypeId(typeId);
        //先算出来当前应该加载的数据是哪些先算出来当前应该加载的数据是哪些,然后进行查询 sql
        int pageSize = pageBean.getPageSize();//每页的数量
        List<TbGoods> goodsList = goodsDao.getGoodsByTypeIdAndLimit(typeId, (currentPage - 1) * pageSize, pageSize);
        pageBean.setTotalPage(goodsCountByTypeId);//设置总条数
        pageBean.setList(goodsList);//设置查询到数据
        System.err.println(goodsCountByTypeId);
        DBUtils.close();
        return pageBean;
    }

    @Override
    public TbGoods getGoodById(int id) {
        if (id<=0){
            return null;
        }
        return goodsDao.getGoodById(id);
    }
}
